/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <windows.h>
#include <XInput.h>
#pragma comment(lib,"XInput.lib")

#include "XInputManager.h"

#include "hhdVector2.h"

namespace hhd
{
    XINPUT_STATE* XInputManager::_xbox_controller_states;
    XINPUT_VIBRATION* XInputManager::_xbox_controller_vibration;

    unsigned int* XInputManager::_button_states_0 = NULL;
    unsigned int* XInputManager::_button_states_1 = NULL;
    unsigned char** XInputManager::_trigger_states_0 = NULL;
    unsigned char** XInputManager::_trigger_states_1 = NULL;
    short** XInputManager::_thumbstick_states_0 = NULL;
    short** XInputManager::_thumbstick_states_1 = NULL;

    bool* XInputManager::_states_changed = NULL;

    void XInputManager::initialize()
    {
        _xbox_controller_states = new XINPUT_STATE[4];
        _xbox_controller_vibration = new XINPUT_VIBRATION[4];

        _button_states_0 = new unsigned int[4];
        memset(_button_states_0, 0, sizeof(int) * 4);
        _button_states_1 = new unsigned int[4];
        memset(_button_states_1, 0, sizeof(int) * 4);
        _trigger_states_0 = new unsigned char*[4];
        _trigger_states_1 = new unsigned char*[4];
        _thumbstick_states_0 = new short*[4];
        _thumbstick_states_1 = new short*[4];



        for(int k = 0; k < 4; k++)
        {
            _trigger_states_0[k] = new unsigned char[2];
            memset(_trigger_states_0[k], 0, 2);
            _trigger_states_1[k] = new unsigned char[2];
            memset(_trigger_states_1[k], 0, 2);
            _thumbstick_states_0[k] = new short[4];
            memset(_thumbstick_states_0[k], 0, sizeof(short) * 4);
            _thumbstick_states_1[k] = new short[4];
            memset(_thumbstick_states_1[k], 0, sizeof(short) * 4);
        }

        _states_changed = new bool[4];
    }

    void XInputManager::finalize()
    {
        delete[] _xbox_controller_states;
        delete[] _xbox_controller_vibration;
        delete[] _button_states_0;
        delete[] _button_states_1;
        for(int k = 0; k < 4; k++)
        {
            delete[] _trigger_states_0[k];
            delete[] _trigger_states_1[k];
            delete[] _thumbstick_states_0[k];
            delete[] _thumbstick_states_1[k];
        }

        delete[] _trigger_states_0;
        delete[] _trigger_states_1;
        delete[] _thumbstick_states_0;
        delete[] _thumbstick_states_1;

        delete[] _states_changed;
    }

    bool XInputManager::pollXboxControllers()
    {
        // swap buffes
        void* temp = _button_states_0;
        _button_states_0 = _button_states_1;
        _button_states_1 = (unsigned int*)temp;

        temp = _trigger_states_0;
        _trigger_states_0 = _trigger_states_1;
        _trigger_states_1 = (unsigned char**)temp;

        temp = _thumbstick_states_0;
        _thumbstick_states_0 = _thumbstick_states_1;
        _thumbstick_states_1 = (short**)temp;

        for(int k = 0; k < 4; k++)
        {
            _states_changed[k] = false;
            if(XInputGetState(k, &_xbox_controller_states[k]) != ERROR_SUCCESS)
            {
                continue;
            }
            else
            {
                _button_states_0[k] = _xbox_controller_states[k].Gamepad.wButtons;

                _trigger_states_0[k][0] = _xbox_controller_states[k].Gamepad.bLeftTrigger;
                _trigger_states_0[k][1] = _xbox_controller_states[k].Gamepad.bRightTrigger;

                _thumbstick_states_0[k][0] = _xbox_controller_states[k].Gamepad.sThumbLX;
                _thumbstick_states_0[k][1] = _xbox_controller_states[k].Gamepad.sThumbLY;

                Vector2 left_stick(_thumbstick_states_0[k][0], _thumbstick_states_0[k][1]);
                if(left_stick.magnitude() < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
                {
                    _thumbstick_states_0[k][0] = _thumbstick_states_0[k][1] = 0;
                }

                _thumbstick_states_0[k][2] = _xbox_controller_states[k].Gamepad.sThumbRX;
                _thumbstick_states_0[k][3] = _xbox_controller_states[k].Gamepad.sThumbRY;

                Vector2 right_stick(_thumbstick_states_0[k][2], _thumbstick_states_0[k][3]);
                if(right_stick.magnitude() < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE )
                {
                    _thumbstick_states_0[k][2] = _thumbstick_states_0[k][3] = 0;
                }



                if(_button_states_0[k] != _button_states_1[k])
                {
                    _states_changed[k] = true;
                    continue;
                }

                if(*(short*)_trigger_states_0[k] != *(short*)_trigger_states_1[k])
                {
                    _states_changed[k] = true;
                    continue;
                }

                if(*(long long*)_thumbstick_states_0[k] != *(long long*)_thumbstick_states_1[k])
                {
                    _states_changed[k] = true;
                    continue;
                }
            }
        }
        int as_int = *(int*)_states_changed;
        return as_int != 0;
    }

    void XInputManager::setVibration(Input::XBoxController in_controller, float left_motor_speed, float right_motor_speed)
    {
        XINPUT_VIBRATION xv;
        xv.wLeftMotorSpeed = (unsigned short)(left_motor_speed * 65535);
        xv.wRightMotorSpeed = (unsigned short)(right_motor_speed * 65535);

        XInputSetState((int)in_controller, &xv);
    }
}
